<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    :fullscreen="true">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="联系人" prop="username">
      <el-input v-model="dataForm.username" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="请输入手机号"></el-input>
    </el-form-item>
    <el-form-item label="所属地区" prop="regionId">
      <el-cascader style="width: 600px;"  change-on-select  :options="options" v-model="dataForm.regionId" :props="optionProps" placeholder="所属地区输入"  clearable></el-cascader>
      <!--<el-select
        v-model="dataForm.sheng"
        @change="choseProvince"
        hidden
        value="">
      <el-option
        v-for="item in province"
        :key="item.value"
        :label="item.label"
        :value="item.value">
      </el-option>
      </el-select>
      <el-select
        v-model="dataForm.shi"
        @change="choseCity"
        placeholder="市级地区"
        hidden
        value="">
        <el-option
          v-for="item in choseShi"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-select
        v-model="dataForm.qu"
        @change="choseBlock"
        placeholder="区级地区"
        hidden
        value="">
        <el-option
          v-for="item in choseQu"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>-->
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime" hidden>
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="电子邮箱"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注" type="textarea"></el-input>
    </el-form-item>
<!--    <el-form-item label="" prop="def0" hidden>
      <el-input v-model="dataForm.def0" placeholder=""></el-input>
    </el-form-item>-->
    <el-form-item label="" prop="def1" hidden>
      <el-input v-model="dataForm.def1" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="def2" hidden>
      <el-input v-model="dataForm.def2" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="def3" hidden>
      <el-input v-model="dataForm.def3" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="def4" hidden>
      <el-input v-model="dataForm.def4" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="isLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import axios from 'axios'

  export default {
    data () {
      return {
        isLoading: false,
        visible: false,
        mapJson: window.SITE_CONFIG.cdnUrl + '/static/plugins/division/city.json',
        options: [],
        province: [],
        choseShi: [],
        choseQu: [],
        city: '',
        block: '',
        optionProps: {value: 'id', label: 'name', children: 'children'},
        dataForm: {
          id: 0,
          username: '',
          mobile: '',
          regionId: [],
          createTime: '',
          email: '',
          remark: '',
          def0: 0, // 逻辑删除
          def1: '',
          def2: '',
          def3: '',
          sheng: '',
          shi: '',
          qu: ''
        },
        dataRule: {
          username: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            // eslint-disable-next-line standard/object-curly-even-spacing
            { required: false, pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9]|151)[0-9]{8}$/, message: '手机号格式错误', trigger: 'blur'}
          ],
          regionId: [
            { required: false, message: '所属地区不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, type: 'email', message: '邮箱格式错误', trigger: 'blur' }
          ],
          remark: [
            { required: false, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      getCityDatas: function () {
        let that = this
        axios.get(this.mapJson).then(function (response) {
          if (response.status === 200) {
            that.options = response.data
          }
        })
      },
      /* getCityData: function () {
        var that = this
        axios.get(this.mapJson).then(function (response) {
          if (response.status === 200) {
            let data = response.data
            that.province = []
            that.city = []
            that.block = []
            // 省市区数据分类
            data.forEach(item => {
              if (item.value.match(/0000$/)) { // 省
                that.province.push({value: item.value, pid: item.parent, label: item.name, children: []})
              } else if (item.value.match(/00$/)) { // 市
                that.city.push({value: item.value, pid: item.parent, label: item.name, children: []})
              } else { // 区
                that.block.push({value: item.value, pid: item.parent, label: item.name})
              }
            })
            // 分类市级
            that.province.forEach(province => {
              that.city.forEach(city => {
                if (province.value === city.pid) {
                  province.children.push(city)
                }
              })
            })
            // 分类区级
            that.city.forEach(city => {
              that.block.forEach(block => {
                if (city.value === block.pid) {
                  city.children.push(block)
                }
              })
            })
            console.log(this.province)
          } else {
            console.log(response.status)
          }
        }).catch(function (error) { console.log(error) })
      },
      // 选省
      choseProvince: function (e) {
        this.province.forEach(choseProvince => {
          if (e === choseProvince.value) {
            this.dataForm.shi = ''
            this.dataForm.qu = ''
            this.choseShi = choseProvince.children
            this.E = this.choseQu[0].value
          }
        })
      },
      // 选市
      choseCity: function (e) {
        this.city.forEach(choseCity => {
          if (e === choseCity.value) {
            this.dataForm.qu = ''
            this.choseQu = choseCity.children
            this.E = this.choseQu.value
          }
        })
      },
      // 选区
      choseBlock: function (e) {
        this.E = e
      }, */
      // 转换字符串数组为整型数组
      changeStringArray: function (e) {
        let dataStrArr = e.split(',')
        const dataIntArr = dataStrArr.map(item => {
          return +item
        })
        return dataIntArr
      },
      init (id) {
        this.dataForm.id = id || 0
        this.isLoading = false
        this.visible = true
        this.getCityDatas()
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/addressbook/addressbook/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.addressBook.username
                this.dataForm.mobile = data.addressBook.mobile
                this.dataForm.regionId = this.changeStringArray(data.addressBook.regionId)
                this.dataForm.createTime = data.addressBook.createTime
                this.dataForm.email = data.addressBook.email
                this.dataForm.remark = data.addressBook.remark
                this.dataForm.def0 = data.addressBook.def0
                this.dataForm.def1 = data.addressBook.def1
                this.dataForm.def2 = data.addressBook.def2
                this.dataForm.def3 = data.addressBook.def3
                this.dataForm.def4 = data.addressBook.def4
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoading = true
            this.$http({
              url: this.$http.adornUrl(`/addressbook/addressbook/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'mobile': this.dataForm.mobile,
                'regionId': this.dataForm.regionId.toString(),
                'createTime': this.dataForm.createTime,
                'email': this.dataForm.email,
                'remark': this.dataForm.remark,
                'def0': this.dataForm.def0,
                'def1': this.dataForm.def1,
                'def2': this.dataForm.def2,
                'def3': this.dataForm.def3,
                'def4': this.dataForm.def4
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
